package com.shelpe.services.sinbad.controller.dto;

import java.util.Date;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.shelpe.services.sinbad.entity.jpa.Adgroup;
import com.shelpe.services.sinbad.entity.jpa.Campaign;
import com.shelpe.services.sinbad.entity.jpa.Tactics;
import com.shelpe.services.sinbad.entity.jpa.WantRank;
import com.shelpe.services.sinbad.model.Keyword;
import com.shelpe.services.sinbad.utils.DateUtil;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = true)
@JsonInclude(Include.NON_DEFAULT)
public class RankingView extends RankingDto {
	private final String DEVICE_MOBILE = "mobile";
	private final String DEVICE_PC = "pc";
	private final String DEVICE_ALL = "all";

	@JsonProperty("keyword_id")
	private long keywordID;
	private String word;
	@JsonProperty("campaign_id")
	private long campaignID;
	@JsonProperty("campaign_name")
	private String campaignName;
	@JsonProperty("campaign_auto_type")
	private String campaignAutoType;
	@JsonProperty("adgroup_id")
	private long adgroupID;
	@JsonProperty("adgroup_name")
	private String adgroupName;
	@JsonProperty("original_price")
	private int originalPrice;
	@JsonProperty("want_rank")
	private Integer wantRank;

	/** below update keyword ***/
	@JsonProperty("match_scope")
	private int matchScope;
	@JsonProperty("max_price")
	private int maxPrice;
	@JsonProperty("is_default_price")
	private Integer isDefaultPrice;
	@JsonProperty("max_mobile_price")
	private int maxMobilePrice;// 是否采用pc*无线溢价，还是独立价格（1：是；0：否)
	@JsonProperty("mobile_is_default_price")
	private Integer mobileIsDefaultPrice;

	@Override
	public void from(Tactics po) {
		super.from(po);
	}

	@Override
	public Tactics to() {
		Tactics po = super.to();
		return po;
	}

	public void from(Campaign campaign, Adgroup adgroup, Keyword kw, Tactics tactics, WantRank wantRank) {
		this.setKeywordID(kw.getKeywordId());
		this.setWord(kw.getWord());
		this.maxPrice = kw.getMaxPrice();
		this.matchScope = kw.getMatchscope();
		if (kw.getIsDefaultPrice()) {
			this.isDefaultPrice = 1;
		} else {
			this.isDefaultPrice = 0;
		}
		this.mobileIsDefaultPrice = kw.getMobileIsDefaultPrice();
		this.maxMobilePrice = kw.getMaxMobilePrice();
		if (kw.getMobileIsDefaultPrice() == 1) {// 溢价
			this.maxMobilePrice = kw.getMaxPrice() * adgroup.getMobileDiscount() / 100;
		}

		this.setAdgroupID(adgroup.getAutoID());
		this.setAdgroupName(adgroup.getProductName());
		this.setCampaignID(campaign.getAutoID());
		this.setCampaignName(campaign.getTitle());
		this.setCampaignAutoType(campaign.getAutoType().name());
		this.setWantRank(wantRank.getWantRank());
		this.setOriginalPrice(wantRank.getOriginalValue());
		this.setHeighestPrice(tactics.getHeighestPrice());
		this.setRankingID(tactics.getTacticsID());
		if (tactics.getStatus() == Tactics.Status.starting) {
			this.setStatus("open");
		} else if (tactics.getStatus() == Tactics.Status.pause) {
			this.setStatus("pause");
		} else if (tactics.getStatus() == Tactics.Status.finish) {
			this.setStatus("finish");
		}
		if (tactics.getToTime() != null) {
			if (DateUtil.checkTime(new Date(), tactics.getToTime()) >= 0) {
				this.setStatus("expired");
			}
		}
		if (Tactics.DEVICE_MOBILE == tactics.getDevice()) {
			this.setDevice(DEVICE_MOBILE);
		} else {
			this.setDevice(DEVICE_PC);
		}
	}
}
